package com.control;

import com.dao.UserDao;
import com.dao.UserDaoImpl;
import com.model.User;

import javax.servlet.DispatcherType;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/addStudentInfo.do")
public class addStudentInfoServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = new User();
        user.setId(Integer.parseInt(request.getParameter("id")));
        user.setUsername(request.getParameter("username"));
        user.setScore(Double.parseDouble(request.getParameter("score")));
        UserDao userDao = new UserDaoImpl();
        String message = "";
        if (userDao.hasUser(user)) {
            if (userDao.updateUserInfo(user)) {
                message = "更新用户信息成功";
            } else {
                message = "更新用户信息失败";
            }
        } else {
            if (userDao.addUser(user)) {
                message = "插入用户信息成功";
            } else {
                message = "插入用户信息失败";
            }

        }
        request.setAttribute("message", message);
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("addStudentInfo.jsp");
        requestDispatcher.forward(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
